package com.hotec.PROJECT_NAME.modules.sys.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.hotec.PROJECT_NAME.component.basic.BaseService;
import com.hotec.PROJECT_NAME.modules.sys.entity.SysMessage;
import com.hotec.PROJECT_NAME.modules.sys.entity.SysMessageRead;
import com.hotec.PROJECT_NAME.modules.sys.mapper.SysMessageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;


/**
 * @author llb
 * @date
 */
@Service
public class SysMessageService extends BaseService<SysMessage, SysMessageMapper> {
    private static final String SYSTEM = "PROJECT_NAME";

    @Autowired
    SysMessageReadService sysMessageReadService;


    public void pushMessage(String targetId, String type, String system, String content) {
        super.save(
                new SysMessage()
                    .setTargetId(targetId)
                    .setType(type)
                    .setContent(content)
                    .setSystem(system)
        );
    }

    private void commonUpdateRead(List<SysMessage> sysMessageList) {
        if (CollectionUtil.isNotEmpty(sysMessageList)) {
            SysMessage sysMessage = sysMessageList.get(0);

            Date newDate = sysMessage.getCreateTime();


            Date newReadTime = sysMessageReadService.queryNewReadTime(sysMessage.getTargetId());

            if(ObjectUtil.isNull(newReadTime) || newDate.getTime() >= newReadTime.getTime()){
                sysMessageReadService.save(
                        new SysMessageRead()
                                .setAccountId(getCurrentAccountId(true))
                                .setTargetId(sysMessage.getTargetId())
                                .setType(sysMessage.getType())
                );
            }
        }
    }

    public List<SysMessage> trustQueryTop5() {
        List<SysMessage> sysMessageList = super.query(
                1,
                5,
                "create_time desc",
                new SysMessage()
                        .setSystem(SYSTEM)
        ).getList();

        commonUpdateRead(sysMessageList);

        return sysMessageList;
    }

    private Long commonQueryNewCount(String targetId, Date newReadTime) {
        if(ObjectUtil.isNotNull(newReadTime)){
            LinkedHashMap<String, Object> querySysMessageCondition = Maps.newLinkedHashMap();
            querySysMessageCondition.put("createTime:GT", DateUtil.formatDateTime(newReadTime));
            querySysMessageCondition.put("targetId", targetId);

            return super.count(querySysMessageCondition);
        }

        LinkedHashMap<String, Object> querySysMessageCondition = Maps.newLinkedHashMap();
        querySysMessageCondition.put("targetId", targetId);

        return super.count(querySysMessageCondition);
    }

    public Long trustNewCount() {
        String targetId = getCurrentAccountShopId(true);

        Date newReadTime = sysMessageReadService.queryNewReadTime(targetId);

        return commonQueryNewCount(targetId, newReadTime);
    }

    public List<SysMessage> shopQueryTop5() {
        List<SysMessage> sysMessageList = super.query(
                1,
                5,
                "create_time desc",
                new SysMessage()
                        .setSystem(SYSTEM)
                        .setTargetId(getCurrentAccountShopId(true))
        ).getList();

        commonUpdateRead(sysMessageList);

        return sysMessageList;
    }

    public Long shopNewCount() {
        String currentAccountShopId = getCurrentAccountShopId();

        if(StrUtil.isBlank(currentAccountShopId)) return 0L;

        Date newReadTime = sysMessageReadService.queryNewReadTime(currentAccountShopId);

        return commonQueryNewCount(currentAccountShopId, newReadTime);
    }

    private PageInfo<SysMessage> commonQueryMessagePage(Integer pageNum, Integer pageSize, LinkedHashMap<String, Object> entity) {
        PageInfo<SysMessage> sysMessagePageInfo = super.queryByPage(entity, pageNum, pageSize, "create_time desc");

        List<SysMessage> messagePageInfoList = sysMessagePageInfo.getList();

        commonUpdateRead(messagePageInfoList);

        return sysMessagePageInfo;
    }

    public PageInfo<SysMessage> shopQueryPage(Integer pageNum, Integer pageSize, LinkedHashMap<String, Object> entity) {
        entity.put("system", SYSTEM);
        entity.put("targetId", getCurrentAccountShopId(true));

        return commonQueryMessagePage(pageNum, pageSize, entity);
    }
}
